<?php

namespace lh\controller;

use \GatewayWorker\Lib\Gateway;

class Room{
  //加入
  static function enter($cid,$id){
    //是否已经在该房间
    if(isset(\LH::$hallUser[$cid]['room'])&&\LH::$hallUser[$cid]['room']==$id){
      $result=json_encode(['r'=>'/main/error','code' => 40,'msg' => '你已经在房间中']);
      Gateway::sendToClient($cid, $result);
      return;
    }
    //判断房间人数
    if(\LH::$roomList[$id]['now']>=4){
      $result=json_encode(['r'=>'/main/error','code' => 40,'msg' => '房间满员']);
      Gateway::sendToClient($cid, $result);
      return;
    }

    //加入房间
    \LH::$hallUser[$cid]['room']=$id;
    Gateway::joinGroup($cid, $id);

    //返回房间ID
    $result=json_encode(['r'=>'/room/enter/ok','data'=>$id]);
    Gateway::sendToClient($cid, $result);

    //通知房间更新数据
    \LH::$roomList[$id]['now']++;
    \LH::$roomList[$id]['user'][$cid]=['hero'=>'','ready'=>false];
    $room=\LH::$roomList[$id];
    $data=['id'=>$id,'user'=>$room['user']];
    $result=json_encode(['r'=>'/room/update','data'=>$data]);
    Gateway::sendToGroup($id,$result);

    //通知大厅更新房间
    $data=['id'=>$id,'name'=>$room['name'],'now'=>$room['now'],'num'=>4];
    $result=json_encode(['r'=>'/hall/room/update','data'=>$data]);
    Gateway::sendToGroup('hall',$result);

    //通知取消倒计时
    $result=json_encode(['r'=>'/room/unstart']);
    Gateway::sendToGroup($id,$result);
  }

  //离开
  static function leave($cid){
    //是否已经离开
    if(!isset(\LH::$hallUser[$cid]['room'])){
      return;
    }

    $roomid=\LH::$hallUser[$cid]['room'];
    
    //离开房间
    unset(\LH::$hallUser[$cid]['room']);
    Gateway::leaveGroup($cid, $roomid);
    \LH::$roomList[$roomid]['now']--;
    unset(\LH::$roomList[$roomid]['user'][$cid]);

    //如果房间人数为0
    if(\LH::$roomList[$roomid]['now']==0){
      unset(\LH::$roomList[$roomid]);
      $result=json_encode(['r'=>'/hall/room/del','data'=>$roomid]);
      Gateway::sendToGroup('hall',$result);
      return;
    }

    //通知房间
    $room=\LH::$roomList[$roomid];
    $data=['id'=>$roomid,'user'=>$room['user']];
    $result=json_encode(['r'=>'/room/update','data'=>$data]);
    Gateway::sendToGroup($roomid,$result);
    
    //通知大厅更新
    $data=['id'=>$roomid,'name'=>$room['name'],'now'=>$room['now'],'num'=>4];
    $result=json_encode(['r'=>'/hall/room/update','data'=>$data]);
    Gateway::sendToGroup('hall',$result);
  }

  //聊天
  static function chat($cid,$content){
    $roomid=\LH::$hallUser[$cid]['room'];
    $data=['name'=>\LH::$hallUser[$cid]['name'],'content'=>$content];
    $result=json_encode(['r'=>'/room/chat','data'=>$data]);
    Gateway::sendToGroup($roomid,$result);
  }

  //修改英雄
  static function hero($cid,$hero){
    $roomid=\LH::$hallUser[$cid]['room'];
    \LH::$roomList[$roomid]['user'][$cid]['hero']=$hero;

    //通知房间
    $room=\LH::$roomList[$roomid];
    $data=['id'=>$roomid,'user'=>$room['user']];
    $result=json_encode(['r'=>'/room/update','data'=>$data]);
    Gateway::sendToGroup($roomid,$result);
  }

  //准备
  static function ready($cid){
    //检查是否有选择英雄
    $roomid=\LH::$hallUser[$cid]['room'];
    if(!\LH::$roomList[$roomid]['user'][$cid]['hero']){
      $result=json_encode(['r'=>'/main/error','code' => 40,'msg' => '请选择英雄']);
      Gateway::sendToClient($cid, $result);
      return;
    }
    
    //处理roomList
    \LH::$roomList[$roomid]['user'][$cid]['ready']=true;

    //通知房间
    $room=\LH::$roomList[$roomid];
    $data=['id'=>$roomid,'user'=>$room['user']];
    $result=json_encode(['r'=>'/room/update','data'=>$data]);
    Gateway::sendToGroup($roomid,$result);

    //判断是否所有人都准备了
    $allReady=true;
    foreach ($room['user'] as $value) {
      if($value['ready']==false){
        $allReady=false;
        break;
      }
    }
    //返回随机key
    if($allReady){
      $random=\lh\method\Main::randomStr(10);
      $key=time().$random;
      $result=json_encode(['r'=>'/room/start','data'=>$key]);
      Gateway::sendToGroup($roomid,$result);
    }
  }

  //取消准备
  static function unReady($cid){
    $roomid=\LH::$hallUser[$cid]['room'];
    \LH::$roomList[$roomid]['user'][$cid]['ready']=false;

    //通知房间
    $room=\LH::$roomList[$roomid];
    $data=['id'=>$roomid,'user'=>$room['user']];
    $result=json_encode(['r'=>'/room/update','data'=>$data]);
    Gateway::sendToGroup($roomid,$result);

    //通知取消倒计时
    $result=json_encode(['r'=>'/room/unstart']);
    Gateway::sendToGroup($roomid,$result);
  }
}